package com.jt.tenant.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jt.tenant.R;
import com.jt.tenant.conn.SetInsSup;
import com.jt.tenant.dialog.TipDialog;
import com.jt.tenant.widget.ExpenseTypeSpinner;
import com.jt.tenant.widget.Tools;
import com.jt.tenant.widget.TypeSpinner;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by AD on 2016/5/12.
 */
public class InsSupInfoActivity extends JTActivity implements OnClickListener,Serializable {

    private TextView title,commit,next;

    private LinearLayout call;

    private ImageView iv_insurant,iv_insure,iv_consignee,iv_agreement;

    private EditText et_insurant_name,et_insurant_idcard,et_insurant_phone,et_insurant_email;

    private EditText et_insure_name,et_insure_idcard,et_insure_phone,et_insure_email;

    private ExpenseTypeSpinner consignee_spinner;//ins_consignee_get_spinner

    private EditText et_consignee_name,et_consignee_phone,et_consignee_address;

    private TextView agreement_1,agreement_2;//tv_ins_sup_agreement_1

    private int identical_flag_1 = 0,identical_flag_2 = 0,identical_flag_3 = 0,agreement_flag = 0,send_type = 0;//三个一致选项的标志位,协议标志位,保单寄送方式

    private List<String> typeList = new ArrayList<>();

    private SetInsSup setInsSup = new SetInsSup(new AsyCallBack<SetInsSup.Info>(){

        @Override
        public void onSuccess(String toast, int type, SetInsSup.Info info) throws Exception {

            super.onSuccess(toast, type, info);

            if(info.msg.equals("000")){

                startActivity(new Intent(context,InsConfirmOrderActivity.class));

            }else {

                UtilToast.show(context,info.msg);

            }
        }
    });

    private String ins_id,name,phone;

    public static CommitInfo commitInfo;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_ins_sup_info);

        ins_id = getIntent().getStringExtra("ins_id");

        name = getIntent().getStringExtra("name");

        phone = getIntent().getStringExtra("phone");

        initView();

    }

    private void initView() {

        (title = (TextView)findViewById(R.id.tv_title_name)).setText("补充保单信息");

        (commit = (TextView)findViewById(R.id.commit)).setText("取消查询");

        (call = (LinearLayout) findViewById(R.id.lin_ins_call)).setOnClickListener(this);

        (next = (TextView)findViewById(R.id.tv_ins_sup_next)).setOnClickListener(this);

        (iv_insurant = (ImageView)findViewById(R.id.iv_ins_sup_insurant)).setOnClickListener(this);

        (iv_insure = (ImageView)findViewById(R.id.iv_ins_sup_insure)).setOnClickListener(this);

        (iv_consignee = (ImageView)findViewById(R.id.iv_ins_sup_consignee)).setOnClickListener(this);

        (iv_agreement = (ImageView)findViewById(R.id.iv_ins_sup_agreement)).setOnClickListener(this);

        et_insurant_name = (EditText)findViewById(R.id.et_insurant_name);

        et_insurant_name.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {


            }

            @Override
            public void afterTextChanged(Editable s) {

                if(identical_flag_2 == 1){

                    et_insure_name.setText(s);

                }

                if(identical_flag_3 == 1){

                    et_consignee_name.setText(s);

                }


            }
        });

        et_insurant_idcard = (EditText)findViewById(R.id.et_insurant_idcard);

        et_insurant_idcard.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {


            }

            @Override
            public void afterTextChanged(Editable s) {

                if(identical_flag_2 == 1){

                    et_insure_idcard.setText(s);

                }


            }
        });

        et_insurant_phone = (EditText)findViewById(R.id.et_insurant_phone);

        et_insurant_phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {


            }

            @Override
            public void afterTextChanged(Editable s) {

                if(identical_flag_2 == 1){

                    et_insure_phone.setText(s);

                }

                if(identical_flag_3 == 1){

                    et_consignee_phone.setText(s);

                }


            }
        });

        et_insurant_email = (EditText)findViewById(R.id.et_insurant_email);

        et_insurant_email.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {


            }

            @Override
            public void afterTextChanged(Editable s) {

                if(identical_flag_2 == 1){

                    et_insure_email.setText(s);

                }

            }
        });

        et_insure_name = (EditText)findViewById(R.id.et_insure_name);

        et_insure_idcard = (EditText)findViewById(R.id.et_insure_idcard);

        et_insure_phone = (EditText)findViewById(R.id.et_insure_phone);

        et_insure_email = (EditText)findViewById(R.id.et_insure_email);

        consignee_spinner = (ExpenseTypeSpinner)findViewById(R.id.ins_consignee_get_spinner);

        consignee_spinner.setString("请选择");

        typeList.add("电子保单");

        typeList.add("纸质保单");

        consignee_spinner.createItem(typeList);

        consignee_spinner.setOnItemClickListener(new TypeSpinner.OnSpinnerClickListener<String>() {
            @Override
            public void onShow() {

                consignee_spinner.dismiss();

            }

            @Override
            public void onItemClick(String s) {

                if(s.equals("电子保单")){

                    send_type = 0;

                }else {

                    send_type = 1;

                }

            }
        });

        et_consignee_name = (EditText)findViewById(R.id.et_consignee_name);

        et_consignee_phone = (EditText)findViewById(R.id.et_consignee_phone);

        et_consignee_address = (EditText)findViewById(R.id.et_consignee_address);

        (agreement_1 = (TextView)findViewById(R.id.tv_ins_sup_agreement_1)).setOnClickListener(this);

        (agreement_2 = (TextView)findViewById(R.id.tv_ins_sup_agreement_2)).setOnClickListener(this);

        commitInfo = new CommitInfo() {
            @Override
            public void commit() {

                setInsSup.orderId = ins_id;

                setInsSup.phname = et_insurant_name.getText().toString().trim();

                setInsSup.phidcard = et_insurant_idcard.getText().toString().trim();

                setInsSup.phphone = et_insurant_phone.getText().toString().trim();

                setInsSup.phemail = et_insurant_email.getText().toString().trim();

                setInsSup.owner_name = et_insure_name.getText().toString().trim();

                setInsSup.idcard = et_insure_idcard.getText().toString().trim();

                setInsSup.phone = et_insure_phone.getText().toString().trim();

                setInsSup.email = et_insure_email.getText().toString().trim();

                setInsSup.outType = String.valueOf(send_type);

                setInsSup.recipients = et_consignee_name.getText().toString().trim();

                setInsSup.recipientsPhone = et_consignee_phone.getText().toString().trim();

                setInsSup.acceptAddress = et_consignee_address.getText().toString().trim();

                setInsSup.execute(context);

            }
        };


    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){

            case R.id.iv_ins_sup_insurant:

                if(identical_flag_1 == 0){

                    iv_insurant.setImageResource(R.mipmap.icon_pink_check);

                    identical_flag_1 = 1;

                    et_insurant_name.setText(name);

                    et_insurant_name.setEnabled(false);

                    et_insurant_phone.setText(phone);

                    et_insurant_phone.setEnabled(false);

                }else {

                    iv_insurant.setImageResource(R.mipmap.icon_pink_uncheck);

                    identical_flag_1 = 0;

                }



                break;

            case R.id.iv_ins_sup_insure:

                if(identical_flag_2 == 0){

                    iv_insure.setImageResource(R.mipmap.icon_pink_check);

                    identical_flag_2 = 1;

                    et_insure_name.setText(et_insurant_name.getText().toString().trim());

                    et_insure_name.setEnabled(false);

                    et_insure_idcard.setText(et_insurant_idcard.getText().toString().trim());

                    et_insure_idcard.setEnabled(false);

                    et_insurant_phone.setText(et_insurant_phone.getText().toString().trim());

                    et_insurant_phone.setEnabled(false);

                    et_insure_email.setText(et_insurant_email.getText().toString().trim());

                    et_insure_email.setEnabled(false);

                }else {

                    iv_insure.setImageResource(R.mipmap.icon_pink_uncheck);

                    identical_flag_2 = 0;

                    et_insure_name.setEnabled(true);

                    et_insure_idcard.setEnabled(true);

                    et_insurant_phone.setEnabled(true);

                    et_insure_email.setEnabled(true);

                }


                break;

            case R.id.iv_ins_sup_consignee:

                if(identical_flag_3 == 0){

                    iv_consignee.setImageResource(R.mipmap.icon_pink_check);

                    identical_flag_3 = 1;

                    et_consignee_name.setText(et_insurant_name.getText().toString().trim());

                    et_consignee_name.setEnabled(false);

                    et_consignee_phone.setText(et_insurant_phone.getText().toString().trim());

                    et_consignee_name.setEnabled(false);

                }else {

                    iv_consignee.setImageResource(R.mipmap.icon_pink_uncheck);

                    identical_flag_3 = 0;

                    et_consignee_name.setEnabled(true);

                    et_consignee_phone.setEnabled(true);

                }



                break;

            case R.id.iv_ins_sup_agreement:

                if(agreement_flag == 0) {

                    iv_agreement.setImageResource(R.mipmap.icon_pink_check);

                    agreement_flag = 1;

                }else {

                    iv_agreement.setImageResource(R.mipmap.icon_pink_uncheck);

                    agreement_flag = 0;

                }

                break;

            case R.id.lin_ins_call:

                Tools.Call(context,"4006115616");

                break;


            case  R.id.tv_ins_sup_next:

                TipDialog inssupDialog = new TipDialog(context);

                inssupDialog.setTip("请仔细核对保单信息，提交后将无法修改。");

                inssupDialog.setType(7);

                inssupDialog.show();

                break;

        }


    }

    public abstract class CommitInfo {

        public abstract void commit();

    }



}
